package cn.itsource.org.controller;

import cn.itsource.basic.annotation.PreAuthorize;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//@ResponseBody+@Controller
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

//    @RequestMapping(value = "/queryById?id=1&name=zhangsan",method = RequestMethod.GET)
    /*
    * @PathVariable  从url地址中取值
    * 此时请求方式：get请求 http://localhost:8080/dept/3
    * */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryById(@PathVariable @ApiParam(name="ID属性",required = true) Long id){
        return departmentService.queryById(id);
    }

    /*查询所有
    * 此时请求方式：get请求 http://localhost:8080/dept
    * */
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    /*分页查询
    *此时请求方式：post请求 http://localhost:8080/dept
    * @RequestBody:从请求体中获取json格式的参数
    * @RequestParam:从请求体中或者url路径中获取 username=zs&password=123 键值对格式的参数
    * */
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /*根据id删除一条数据
        此时请求方式：delete请求 http://localhost:8080/dept
    * */
    @DeleteMapping("/{id}")
    //自定义的注解 代表访问这个接口 /dept/{id} 需要department:delete，这个权限的名字叫删除部门
    @PreAuthorize(name = "删除部门",value = "department:delete")
    public AjaxResult deleteById(@PathVariable Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /*修改和新增
    * 此时请求方式：put请求 http://localhost:8080/dept
    * */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /*批量删除*/
    @PatchMapping

    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }

    }

    /*获取部门树*/
    @GetMapping("/getTree")
    public List<Department> getTree(){
        return departmentService.getTree();
    }



}
